package com.liuche.homechat.common.common.listener;

import com.liuche.homechat.common.common.model.dto.RocketMQConfigProperties;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

@Slf4j
@Component
@RocketMQMessageListener(topic = "",
        consumerGroup = "${rocketmq.consumer.group.common}",
        consumeMode = ConsumeMode.CONCURRENTLY,
        messageModel = MessageModel.CLUSTERING
        )
public class PushMessageListener implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
        @Resource
        private RocketMQConfigProperties prop;

        @Override
        @SneakyThrows
        public void prepareStart(DefaultMQPushConsumer consumer) {
                // 订阅主题
                consumer.subscribe(prop.getTopic().get("common"), "*");
                // 设置开始消费位置
                consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);

        }

        @Override
        public void onMessage(String message) {
                log.info("接受到消息：【{}】", message);
        }
}
